﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RuneLog.Stats;
namespace RuneLog
{
    /// <summary>
    /// Contains a single static method <code>GetLogList(string)</code> which will return a list
    /// of adventurer log entries.  This class cannot be instantiated.
    /// </summary>
    class AdventurersLog
    {
        private AdventurersLog()
        {
        }
        private static String baseURL
        {
            get
            {
                return "http://services.runescape.com/m=adventurers-log/rssfeed?searchName=";
            }
        }
        /// <summary>
        /// Searches for an adventurer's log for the user and populates a list
        /// with its entries.
        /// </summary>
        /// <param name="username">the runescape username</param>
        /// <returns>a list of adventurer's log entries</returns>
        public static List<Entry> GetLogList(string username)
        {
            List<Entry> log = new List<Entry>();
            XmlDocument xmldoc = new XmlDocument();
            try
            {
                xmldoc.Load(baseURL + username);
            }
            catch (Exception)
            {
                throw new UsernameIsNotMemberException(username + " is either not a member or not a valid account.");
            }
            XmlNode baseNode = xmldoc.ChildNodes[2].FirstChild;

            var itemNodes = from XmlNode n in baseNode where n.Name == "item" select n;

            foreach (XmlNode item in itemNodes)
            {

                string title = item.FirstChild.InnerText.Trim();
                string desc = item.ChildNodes[1].InnerText.Trim();
                string link = item.ChildNodes[2].InnerText.Trim();
                string pubDate = item.ChildNodes[3].InnerText.Trim();
                string guid = item.ChildNodes[4].InnerText.Trim();

                log.Add(new Entry(title, desc, link, pubDate, guid));

            }

            return log;
        }
    }
    /// <summary>
    /// Holds information for adventurer's log entries
    /// </summary>
    class Entry
    {
        public String Title
        {
            get;
            private set;
        }

        public String Description
        {
            get;
            private set;
        }

        public String Link
        {
            get;
            private set;
        }

        public DateTime Date
        {
            get;
            private set;
        }

        public String GUID
        {
            get;
            private set;
        }
        public Entry(String title, String desc, String link, String date, String guid)
        {
            Title = title;
            Description = desc;
            Link = link;
            Date = Convert.ToDateTime(date);
            GUID = guid;
       }
    }
}
